package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.manager.service.OrderStatisticsService;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LT
 * @create 2024-03-20-11:29
 */
@Service
public class OrderStatisticsServiceImpl implements OrderStatisticsService {

    @Resource
    OrderInfoMapper orderInfoMapper;

    @Resource
    OrderStatisticsMapper orderStatisticsMapper;

    @Override
    public void generateOrderStatistics(String date) {
        Map<String,Object> map = orderInfoMapper.selectOrderStatistics(date);

        OrderStatistics orderStatistics = new OrderStatistics();
        orderStatistics.setTotalNum(Integer.parseInt(map.get("totalNum").toString()));
        orderStatistics.setTotalAmount(new BigDecimal(map.get("totalAmount").toString()));
        orderStatistics.setOrderDate(date);

        orderStatisticsMapper.saveStatistics(orderStatistics);
    }

    @Override
    public Map<String, Object> list(String begin, String end) {
        Map<String, Object> map = new HashMap<>();
        map.put("totalNumTitle","订单总数量");
        map.put("totalAmountTitle","订单总金额");

        List<OrderStatistics> list = orderStatisticsMapper.getList(begin,end);

        List<String> dateList = list.stream().map(orderStatistics ->
             orderStatistics.getOrderDate()).collect(Collectors.toList());

        List<BigDecimal> amounts = list.stream().map(orderStatistics ->
                orderStatistics.getTotalAmount()).collect(Collectors.toList());

        List<Integer> nums = list.stream().map(orderStatistics ->
                orderStatistics.getTotalNum()).collect(Collectors.toList());
        map.put("dateList",dateList);
        map.put("totalAmountList",amounts);
        map.put("totalNumList",nums);
        return map;
    }
}
